package com.ctz.commonframe.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaRecorder;

import java.io.File;

/**
 * Created by 陈堂正 on 2017/2/21.
 */

public class VoiceRecorder {
    private static final int DEFAULT_MAX_LENGTH = 1000 * 60 * 10;

    private Context context;
    private MediaRecorder mediaRecorder;
    private String voiceFilePath;
    private boolean isStarted;//录音启动标识
    private long prepareTime;
    private long startTime;
    private int maxLength;

    public VoiceRecorder(Context context) {
        this(context, DEFAULT_MAX_LENGTH);
    }

    public VoiceRecorder(Context context, int maxLength){
        this.context = context;
        this.maxLength = maxLength;
    }

    public int getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }

    public String getVoiceFilePath() {
        return voiceFilePath;
    }

    public void setVoiceFilePath(String voiceFilePath) {
        this.voiceFilePath = voiceFilePath;
    }

    public boolean isStarted() {
        return isStarted;
    }

    /**
     * 开始录音
     */
    public boolean start() {
        try {
            if(voiceFilePath == null)
                voiceFilePath = FilePathUtil.getAudioFilePath(context) + FilePathUtil.getAudioFileName();
            isStarted = false;
            File file = new File(voiceFilePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            mediaRecorder = new MediaRecorder();
            // 设置输出文件
            mediaRecorder.setOutputFile(voiceFilePath);
            // 设置音频源为麦克风
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            // 设置音频格式
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
            // 设置音频编码
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            mediaRecorder.setMaxDuration(maxLength);
            prepareTime = System.currentTimeMillis();//录音启动前时间

            mediaRecorder.prepare();
            mediaRecorder.start();

            startTime = System.currentTimeMillis();
            //有延时，则说明系统弹出录音权限确认弹框，则对本次录音操作不做处理
            if (startTime - prepareTime> 1000){
                stop();
                deleteFile();
            }else{
                isStarted = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isStarted;
    }

    /** 获取音量大小 */
    public int getMicLevel(int maxLevel) {
        if (isStarted) {
            try {
                //mMediaRecorder.getMaxAmplitude() 1-32767
                //注意此处mMediaRecorder.getMaxAmplitude 只能取一次，如果前面取了一次，后边再取就为0了
                return ((mediaRecorder.getMaxAmplitude() * maxLevel) / 32768) + 1;
            } catch (Exception e) {
            }

        }
        return 1;
    }

    /**
     * 停止录音
     * @return 录音时长，单位毫秒
     */
    public long stop() {
        if(mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
            isStarted = false;
            return System.currentTimeMillis() - startTime;
        }
        return 0;
    }

    /**
     * 退出录音，删除录音文件
     */
    public void deleteFile(){
        if(voiceFilePath != null){
            File file = new File(voiceFilePath);
            if(file.exists())
                file.delete();
        }
    }
}
